package net.maku.needy.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import net.maku.framework.common.utils.PageResult;
import net.maku.framework.common.utils.Result;
import net.maku.needy.convert.NeedyMemberApplyConvert;
import net.maku.needy.entity.NeedyMemberApplyEntity;
import net.maku.needy.service.NeedyMemberApplyService;
import net.maku.needy.query.NeedyMemberApplyQuery;
import net.maku.needy.vo.NeedyMemberApplyVO;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.List;

/**
* 困难帮扶申请
*
* @author 阿沐 babamu@126.com
* @since 1.0.0 2025-03-20
*/
@RestController
@RequestMapping("needy/needy_member_apply")
@Tag(name="困难帮扶申请")
@AllArgsConstructor
public class NeedyMemberApplyController {
    private final NeedyMemberApplyService needyMemberApplyService;

    @GetMapping("page")
    @Operation(summary = "分页")
    @PreAuthorize("hasAuthority('needy:needy_member_apply:page')")
    public Result<PageResult<NeedyMemberApplyVO>> page(@ParameterObject @Valid NeedyMemberApplyQuery query){
        PageResult<NeedyMemberApplyVO> page = needyMemberApplyService.page(query);

        return Result.ok(page);
    }

    @GetMapping("{id}")
    @Operation(summary = "信息")
    @PreAuthorize("hasAuthority('needy:needy_member_apply:info')")
    public Result<NeedyMemberApplyVO> get(@PathVariable("id") Long id){
        NeedyMemberApplyEntity entity = needyMemberApplyService.getById(id);

        return Result.ok(NeedyMemberApplyConvert.INSTANCE.convert(entity));
    }

    @PostMapping
    @Operation(summary = "保存")
    @PreAuthorize("hasAuthority('needy:needy_member_apply:save')")
    public Result<String> save(@RequestBody NeedyMemberApplyVO vo){
        needyMemberApplyService.save(vo);

        return Result.ok();
    }

    @PutMapping
    @Operation(summary = "修改")
    @PreAuthorize("hasAuthority('needy:needy_member_apply:update')")
    public Result<String> update(@RequestBody @Valid NeedyMemberApplyVO vo){
        needyMemberApplyService.update(vo);

        return Result.ok();
    }

    @DeleteMapping
    @Operation(summary = "删除")
    @PreAuthorize("hasAuthority('needy:needy_member_apply:delete')")
    public Result<String> delete(@RequestBody List<Long> idList){
        needyMemberApplyService.delete(idList);

        return Result.ok();
    }
}